package com.wish.g04.financial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g04.base.entity.*;
import com.wish.g04.base.service.*;
import com.wish.g04.common.core.web.domain.AjaxResult;
import com.wish.g04.common.security.utils.SecurityUtils;
import com.wish.g04.financial.service.IApplyReviewService;
import com.wish.g04.financial.service.IFactoringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 * 房租分期申请 服务实现类
 * </p>
 *
 * @author 薛凯
 * @since 2022-05-23
 */
@Service
public class IApplyReviewServiceImpl implements IApplyReviewService {

    @Autowired
    private IJrRentInstallmentService iJrRentInstallmentService;

    @Autowired
    private ICfContractService iCfContractService;

    @Autowired
    private IBusinessService iBusinessService;

    @Autowired
    private ISfContractService iSfContractService;

    @Autowired
    private IJrSfLoanService iJrSfLoanService;

    @Autowired
    private IFactoringService iFactoringService;

    @Autowired
    private IJrFactoringDetailService iJrFactoringDetailService;

    @Override
    public AjaxResult zuFangDetail(Long id) {
//租房分期详细信息
        JrRentInstallment jrRentInstallmentById = iJrRentInstallmentService.getById(id);
// 获取出房合同，租客信息基本信息
        CfContract cfContractById = iCfContractService.getById(jrRentInstallmentById.getCfContractId());
// 基本信息加载    商户 法人
        Long businessId = cfContractById.getBusinessId();
        Business businessById = iBusinessService.getById(businessId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rent",jrRentInstallmentById);
        map.put("cfContract",cfContractById);
        map.put("business",businessById);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult loans(Long id) {
//收房贷详细信息
        JrSfLoan jrSfLoanById = iJrSfLoanService.getById(id);
// 获取收房合同，租客信息基本信息
        SfContract sfContractById = iSfContractService.getById(jrSfLoanById.getSfContractId());
// 基本信息加载    商户 法人
        Long businessId = sfContractById.getBusinessId();
        Business businessById = iBusinessService.getById(businessId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("jrSfLoan", jrSfLoanById);
        map.put("sfContract", sfContractById);
        map.put("business", businessById);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult factor(Long id) {
        //租约保理申请信息
        JrFactoring jrFactoringById = iFactoringService.getById(id);

        //        租约保理详细信息
        QueryWrapper<JrFactoringDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("factoring_id",id);
        JrFactoringDetail factoringDetailById = iJrFactoringDetailService.getOne(wrapper);
        Long cfContractId = factoringDetailById.getCfContractId();

// 获取出房合同，租客信息基本信息
        CfContract cfContractById = iCfContractService.getById(cfContractId);
// 基本信息加载    商户 法人
        Long businessId = cfContractById.getBusinessId();
        Business businessById = iBusinessService.getById(businessId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("factor", jrFactoringById);
        map.put("cfContract", cfContractById);
        map.put("factorDetail", factoringDetailById);
        map.put("business", businessById);
        return AjaxResult.success(map);
    }

    @Override        //   合同id            审核状态           金融产品
    public AjaxResult review(Long id, Integer applyStatus,Integer jrProductType) {
        Long userId = SecurityUtils.getUserId();
//        租房申请审批
        if(jrProductType==1){
//            租房申请数据
            JrRentInstallment rentInstallment = iJrRentInstallmentService.getById(id);
//            更新审核状态
            rentInstallment.setApplyState(applyStatus)
                    .setUpdateTime(LocalDateTime.now())
                    .setUpdateUserid(userId);
            //            更新
            iJrRentInstallmentService.saveOrUpdate(rentInstallment);

            if(applyStatus==4){//复审通过
//                更新出房合同   未冻结
                CfContract cfContractById = iCfContractService.getById(rentInstallment.getCfContractId());

                iCfContractService.saveOrUpdate(cfContractById.setFreezeStatus(true)
                        .setUpdateTime(LocalDateTime.now())
                        .setUpdateUserid(userId));
            }
        }
//        租房贷
        if(jrProductType==2){
            //租房贷  申请数据
            JrSfLoan jrSfLoanServiceById = iJrSfLoanService.getById(id);
//            更新状态
            jrSfLoanServiceById.setApplyState(applyStatus)
                    .setUpdateUserid(userId)
                    .setUpdateTime(LocalDateTime.now());
//            更新租房贷申请状态
            iJrSfLoanService.saveOrUpdate(jrSfLoanServiceById);
            if(applyStatus==4){//复审通过
//                更新出房合同   未冻结
                SfContract sfContractById = iSfContractService.getById(jrSfLoanServiceById.getSfContractId());
//                更新收房合同     未冻结
                iSfContractService.saveOrUpdate(sfContractById.setFreezeStatus(true)
                        .setUpdateTime(new Date())
                        .setUpdateUserid(userId));
            }
        }
//        租约保理
        if(jrProductType==3){
//            租约保理申请 数据
            JrFactoring jrFactoringById = iFactoringService.getById(id);
//            租约保理  详情  获取出房合同id
            QueryWrapper<JrFactoringDetail> wrapper = new QueryWrapper<>();
            wrapper.eq("factoring_id",id);
            JrFactoringDetail JrFactoringDetailOne = iJrFactoringDetailService.getOne(wrapper);
//            更新状态
            jrFactoringById.setApplyState(applyStatus)
                    .setUpdateTime(LocalDateTime.now())
                    .setUpdateUserid(userId);
//            更新
            iFactoringService.saveOrUpdate(jrFactoringById);
            if(applyStatus==4){//复审通过
//                更新出房合同   未冻结
                CfContract cfContractById = iCfContractService.getById(JrFactoringDetailOne.getCfContractId());
//                更新出房合同     未冻结
                iCfContractService.saveOrUpdate(cfContractById.setFreezeStatus(true)
                        .setUpdateTime(LocalDateTime.now())
                        .setUpdateUserid(userId));
            }
        }
        return AjaxResult.success();
    }
}
